﻿using System;
using System.Data;
using System.Collections.Generic;
using Kingdee.BOS;

namespace KingdeeWebServiceProject.Data
{
    internal static class XDDDBUtils
    {
        private enum DBType
        {
            SQLSERVER = 1,
            ORACLE = 2
        }

        private static readonly string mConnectionString;

        static XDDDBUtils()
        {
            //本地
            //mConnectionString = "Data Source=.;Initial Catalog=AIS20210331114429;User Id=sa;Password=123456;"; //Environments.ConnectionString;
            //测试环境
            //mConnectionString = "Data Source=.;Initial Catalog=AIS20210223095331;User Id=sa;Password=wgny2021;"; 
            //mConnectionString = "Data Source=IN-6E4UP7GO5N6;Initial Catalog=AIS20210523171013;User Id=sa;Password=123qwe!@#;";
            //正式环境
            mConnectionString = "Data Source=192.168.0.192,1433;Initial Catalog=AIS20201127115602_DB;User Id=rdsuser;Password=Wgny20!;"; 
        }

        private static AbstractDatabase GetDBService()
        {
            AbstractDatabase dbService = new SqlDatabase(mConnectionString);

            return dbService;
        }

        //执行SQL语句
        internal static int Execute(string strSql)
        {
            try
            {
                AbstractDatabase dbService = GetDBService();

                return dbService.Execute(strSql);
            }
            catch (Exception e)
            {
                //NLog.LogManager.GetCurrentClassLogger().Info("执行脚本错误：" + strSql);
                throw new Exception(e.Message);
            }
        }

        //执行SQL语句
        internal static int Execute(string strSql, List<SqlParam> lstSqlParams)
        {
            try
            {
                AbstractDatabase dbService = GetDBService();

                return dbService.Execute(strSql, lstSqlParams);
            }
            catch (Exception e)
            {
                //TODO 正式环境最好能放开
                // Logger.Info("小程序脚本错误：", strSql);
                throw new Exception(e.Message);
            }
        }

        //批量执行SQL语句
        internal static int ExecuteBatch(List<string> lstSql)
        {
            AbstractDatabase dbService = GetDBService();

            return dbService.ExecuteBatch(lstSql);
        }

        //批量执行SQL语句
        internal static int ExecuteBatch(List<SqlObject> lstSqlObject)
        {
            AbstractDatabase dbService = GetDBService();

            return dbService.ExecuteBatch(lstSqlObject);
        }

        //执行查询语句，返回DataSet
        internal static DataSet ExecuteDateSet(string strSql)
        {
            try
            {
                AbstractDatabase dbService = GetDBService();

                return dbService.ExecuteDateSet(strSql);
            }
            catch (Exception e)
            {
                //NLog.LogManager.GetCurrentClassLogger().Info("执行脚本错误：" + strSql);
                throw new Exception(e.Message);
            }
        }

        //执行查询语句，返回DataSet
        internal static DataSet ExecuteDateSet(string strSql, List<SqlParam> lstSqlParams)
        {
            try
            {
                AbstractDatabase dbService = GetDBService();

                return dbService.ExecuteDateSet(strSql, lstSqlParams);
            }
            catch (Exception e)
            {
                //NLog.LogManager.GetCurrentClassLogger().Info("执行脚本错误：" + strSql);
                throw new Exception(e.Message);
            }
        }

        // 获取表id值集合
        internal static List<long> GetTableSequence(string tableName, int count)
        {
            List<long> ids = new List<long>();
            string sqlStr = string.Format(@"EXEC CMK_PR_GetTableSequence @TableName = '{0}', @Count = {1} ", tableName, count);
            DataSet ds = ExecuteDateSet(sqlStr);
            if (ds != null && ds.Tables.Count > 0)
            {
                for (int i = 0; i < ds.Tables.Count; i++)
                {
                    DataTable dt = ds.Tables[i];
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        ids.Add(Convert.ToInt64(dt.Rows[0][0]));
                    }
                }
            }
            return ids;
        }
    }
}
